﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PriceLibrary.PriceService;

namespace VDataAdapter.PriceServerAdapter
{
    class TypeConvertor
    {
        public static Dictionary<int, StockInfoDyns> StockBaseToStockInfoDyns(IDictionary<int, StockBase> input)
        {
            var list = new Dictionary<int, StockInfoDyns>();
            try
            {
                foreach (KeyValuePair<int,StockBase> item in input)
                {
                    list.Add(item.Key, (StockInfoDyns)item.Value);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error when converting list: " + ex.Message, ex);
            }
            return list;
        }

        public static Dictionary<string, StockInfoDyns> StockBaseToStockInfoDyns(IDictionary<string, StockBase> input)
        {
            var list = new Dictionary<string, StockInfoDyns>();
            try
            {
                foreach (KeyValuePair<string, StockBase> item in input)
                {
                    list.Add(item.Key, (StockInfoDyns)item.Value);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error when converting list: " + ex.Message, ex);
            }
            return list;
        }

        public static Dictionary<string, StockInfoStatic> StockBaseToStockInfoStatic(IDictionary<string, StockBase> input)
        {
            var list = new Dictionary<string, StockInfoStatic>();
            try
            {
                foreach (KeyValuePair<string, StockBase> item in input)
                {
                    list.Add(item.Key, (StockInfoStatic)item.Value);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error when converting list: " + ex.Message, ex);
            }
            return list;
        }
 
        public static List< StockInfoDyns> StockBaseToStockInfoDyns(IList< StockBase> input)
        {
            var list = new List< StockInfoDyns>();

            try
            {
                foreach (StockBase item in input)
                {
                    list.Add((StockInfoDyns)item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error when converting list: " + ex.Message, ex);
            }

            return list;
        }

        public static Dictionary<int, MarketInfo> StockBaseToMarketInfo(IDictionary<int, StockBase> input)
        {
            var list = new Dictionary<int, MarketInfo>();

            try
            {
                foreach(KeyValuePair<int, StockBase> item in input)
                {
                    list.Add(item.Key, (MarketInfo)item.Value);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error when converting list: " + ex.Message, ex);
            }

            return list;
        }

        public static List<Transaction> StockBaseToTransaction(IList<StockBase> input)
        {
            var list = new List<Transaction>();

            try
            {
                foreach (StockBase item in input)
                {
                    list.Add((Transaction)item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error when converting list: " + ex.Message, ex);
            }

            return list;
        }

        /// <summary>
        /// Convert a List from T1 to T2
        /// </summary>
        /// <typeparam name="T1">input type</typeparam>
        /// <typeparam name="T2">target type</typeparam>
        /// <param name="input"></param>
        /// <exeption>InvalidCastException</exeption>
        /// <returns></returns>
        public static List<T2> ConvertList<T1, T2>(IList<T1> input)
        {
            var result = new List<T2>();
 
            foreach (T1 item in input)
            {
                Object temp = (Object)item;
                result.Add((T2)temp);
            }

            return result;
        }
    }
}
